/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author STEVEN_PHAN
 */
public class DBAccess {

    private static Connection connection;
    private static Statement statement;
    private static ResultSet resultSet;
    private static final String username = "sa";
    private static final String password = "123456";
    private static final String url="jdbc:sqlserver://localhost;databaseName=StudentManagement";

    /**
     * @return the connection
     */
    private  static Connection getConnection() {
        if (connection == null) {
            try {
                connection = DriverManager.getConnection(url, username, password);
            } catch (SQLException ex) {
                Logger.getLogger(DBAccess.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return connection;
    }

    /**
     * @return the statement
     */
    private  static Statement getStatement() {
        if (statement == null) {
            try {
                statement = connection.createStatement();
            } catch (SQLException ex) {
                Logger.getLogger(DBAccess.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return statement;
    }

    public static ResultSet getData(String sql) {
        resultSet = null;
        try {
            getConnection();
            getStatement();
            resultSet = statement.executeQuery(sql);
            if (!resultSet.next()) {
                resultSet = null;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBAccess.class.getName()).log(Level.SEVERE, null, ex);
        }
        return resultSet;
    }
    public static int insertData(String sql){
        int i ;
        i = executeUpdate(sql);
        return i;
    }
    public static int updateData(String sql){
        int i ;
        i = executeUpdate(sql);
        return i;
    }
    public static int deleteData(String sql){
        int i ;
        i = executeUpdate(sql);
        return i;
    }
    private  static int executeUpdate(String sql) {
        int i = -1;
        try {
            getConnection();
            getStatement();
            i = statement.executeUpdate(sql);
        } catch (SQLException ex) {
            Logger.getLogger(DBAccess.class.getName()).log(Level.SEVERE, null, ex);
        }
        return i;
    }
    public static void closeConnect(){
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException ex) {
                Logger.getLogger(DBAccess.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
}
